package com.netty.sever;

import io.netty.buffer.ByteBuf;
import io.netty.channel.*;

import java.util.logging.Logger;

/**
 * @description:
 * @date 2020-04-02 10:40
 * @author: liujinhu
 */
public class OutBoundHandler extends ChannelOutboundHandlerAdapter {
//    private static Logger logger = LoggerFactor.getLogger(OutBoundHandler.class);

    @Override
    public void write(ChannelHandlerContext ctx, Object msg,
                      ChannelPromise promise) throws Exception {

        if (msg instanceof byte[]) {
            byte[] bytesWrite = (byte[])msg;
            ByteBuf buf = ctx.alloc().buffer(bytesWrite.length);
            byte[] req = new byte[buf.readableBytes()];
            buf.readBytes(req);
            String body = new String(req, "UTF-8");
            System.out.println("向设备下发的信息为："+body);

            buf.writeBytes(bytesWrite);
//            ctx.writeAndFlush(buf).addListener((ChannelFutureListener) future -> System.out.println("下发成功！"));
        }else{
            ctx.writeAndFlush(msg);
        }
    }
}
